var t = require("../../../../../../@babel/runtime/helpers/regeneratorRuntime"), e = require("../../../../../../@babel/runtime/helpers/asyncToGenerator"), i = require("../z-paging-utils.js"), a = require("../z-paging-constant.js"), l = require("../z-paging-enum.js"), n = {
    props: {
        useVirtualList: {
            type: Boolean,
            default: i.u.gc("useVirtualList", !1)
        },
        useCompatibilityMode: {
            type: Boolean,
            default: i.u.gc("useCompatibilityMode", !1)
        },
        extraData: {
            type: Object,
            default: function() {
                return i.u.gc("extraData", {});
            }
        },
        useInnerList: {
            type: Boolean,
            default: i.u.gc("useInnerList", !1)
        },
        forceCloseInnerList: {
            type: Boolean,
            default: i.u.gc("forceCloseInnerList", !1)
        },
        cellKeyName: {
            type: String,
            default: i.u.gc("cellKeyName", "")
        },
        innerListStyle: {
            type: Object,
            default: function() {
                return i.u.gc("innerListStyle", {});
            }
        },
        innerCellStyle: {
            type: Object,
            default: function() {
                return i.u.gc("innerCellStyle", {});
            }
        },
        preloadPage: {
            type: [ Number, String ],
            default: i.u.gc("preloadPage", 12),
            validator: function(t) {
                return t <= 0 && i.u.consoleErr("preload-page必须大于0！"), t > 0;
            }
        },
        cellHeightMode: {
            type: String,
            default: i.u.gc("cellHeightMode", l.Enum.CellHeightMode.Fixed)
        },
        virtualListCol: {
            type: [ Number, String ],
            default: i.u.gc("virtualListCol", 1)
        },
        virtualScrollFps: {
            type: [ Number, String ],
            default: i.u.gc("virtualScrollFps", 80)
        }
    },
    data: function() {
        return {
            virtualListKey: i.u.getInstanceId(),
            virtualPageHeight: 0,
            virtualCellHeight: 0,
            virtualScrollTimeStamp: 0,
            virtualList: [],
            virtualPlaceholderTopHeight: 0,
            virtualPlaceholderBottomHeight: 0,
            virtualTopRangeIndex: 0,
            virtualBottomRangeIndex: 0,
            lastVirtualTopRangeIndex: 0,
            lastVirtualBottomRangeIndex: 0,
            virtualItemInsertedCount: 0,
            virtualHeightCacheList: [],
            getCellHeightRetryCount: {
                fixed: 0,
                dynamic: 0
            },
            pagingOrgTop: -1,
            updateVirtualListFromDataChange: !1
        };
    },
    watch: {
        realTotalData: function(t) {
            var e = this;
            this.finalUseVirtualList && (this.updateVirtualListFromDataChange = !0, this.$nextTick(function() {
                e.getCellHeightRetryCount.fixed = 0, !t.length && e._resetDynamicListState(!e.isUserPullDown), 
                t.length && e.cellHeightMode === l.Enum.CellHeightMode.Fixed && e.isFirstPage && e._updateFixedCellHeight(), 
                e._updateVirtualScroll(e.oldScrollTop);
            }));
        },
        virtualList: function(t) {
            this.$emit("update:virtualList", t), this.$emit("virtualListChange", t);
        }
    },
    computed: {
        virtualCellIndexKey: function() {
            return a.c.listCellIndexKey;
        },
        finalUseVirtualList: function() {
            return this.useVirtualList && this.usePageScroll && i.u.consoleErr("使用页面滚动时，开启虚拟列表无效！"), 
            this.useVirtualList && !this.usePageScroll;
        },
        finalUseInnerList: function() {
            return this.useInnerList || this.finalUseVirtualList && !this.forceCloseInnerList;
        },
        finalCellKeyName: function() {
            return this.cellKeyName;
        },
        finalVirtualPageHeight: function() {
            return this.virtualPageHeight > 0 ? this.virtualPageHeight : this.windowHeight;
        },
        virtualRangePageHeight: function() {
            return this.finalVirtualPageHeight * this.preloadPage;
        },
        virtualScrollDisTimeStamp: function() {
            return 1e3 / this.virtualScrollFps;
        }
    },
    methods: {
        doInsertVirtualListItem: function(n, r) {
            var h = this;
            if (this.cellHeightMode === l.Enum.CellHeightMode.Dynamic) {
                this.virtualItemInsertedCount++, n && "[object Object]" === Object.prototype.toString.call(n) || (n = {
                    item: n
                });
                var o = this.virtualCellIndexKey;
                n[o] = "custom-".concat(this.virtualItemInsertedCount), n[a.c.listCellIndexUniqueKey] = "".concat(this.virtualListKey, "-").concat(n[o]), 
                this.totalData.splice(r, 0, n), this.$nextTick(e(t().mark(function e() {
                    var l, s, u, g, c, d, v;
                    return t().wrap(function(t) {
                        for (;;) switch (t.prev = t.next) {
                          case 0:
                            l = 0;

                          case 1:
                            if (!(l <= 10)) {
                                t.next = 17;
                                break;
                            }
                            return t.next = 4, i.u.wait(a.c.delayTime);

                          case 4:
                            return t.next = 6, h._getNodeClientRect("#zp-id-".concat(n[o]), h.finalUseInnerList);

                          case 6:
                            if (s = t.sent) {
                                t.next = 10;
                                break;
                            }
                            return l++, t.abrupt("continue", 15);

                          case 10:
                            for (u = s ? s[0].height : 0, g = h.virtualHeightCacheList[r - 1], c = g ? g.totalHeight : 0, 
                            h.virtualHeightCacheList.splice(r, 0, {
                                height: u,
                                lastTotalHeight: c,
                                totalHeight: c + u
                            }), d = r + 1; d < h.virtualHeightCacheList.length; d++) (v = h.virtualHeightCacheList[d]).lastTotalHeight += u, 
                            v.totalHeight += u;
                            return h._updateVirtualScroll(h.oldScrollTop), t.abrupt("break", 17);

                          case 15:
                            t.next = 1;
                            break;

                          case 17:
                          case "end":
                            return t.stop();
                        }
                    }, e);
                })));
            }
        },
        didUpdateVirtualListCell: function(t) {
            var e = this;
            if (this.cellHeightMode === l.Enum.CellHeightMode.Dynamic) {
                var i = this.virtualHeightCacheList[t];
                this.$nextTick(function() {
                    e._getNodeClientRect("#zp-id-".concat(t), e.finalUseInnerList).then(function(a) {
                        var l = a ? a[0].height : 0, n = l - i.height;
                        i.height = l, i.totalHeight = i.lastTotalHeight + l;
                        for (var r = t + 1; r < e.virtualHeightCacheList.length; r++) {
                            var h = e.virtualHeightCacheList[r];
                            h.totalHeight += n, h.lastTotalHeight += n;
                        }
                    });
                });
            }
        },
        didDeleteVirtualListCell: function(t) {
            if (this.cellHeightMode === l.Enum.CellHeightMode.Dynamic) {
                for (var e = this.virtualHeightCacheList[t], i = t + 1; i < this.virtualHeightCacheList.length; i++) {
                    var a = this.virtualHeightCacheList[i];
                    a.totalHeight -= e.height, a.lastTotalHeight -= e.height;
                }
                this.virtualHeightCacheList.splice(t, 1);
            }
        },
        _virtualListInit: function() {
            var t = this;
            this.$nextTick(function() {
                i.u.delay(function() {
                    t._getNodeClientRect(".zp-scroll-view").then(function(e) {
                        e && (t.pagingOrgTop = e[0].top, t.virtualPageHeight = e[0].height);
                    });
                });
            });
        },
        _updateFixedCellHeight: function() {
            var t = this;
            this.$nextTick(function() {
                i.u.delay(function() {
                    t._getNodeClientRect("#zp-id-0", t.finalUseInnerList).then(function(e) {
                        if (e) t.virtualCellHeight = e[0].height, t._updateVirtualScroll(t.oldScrollTop); else {
                            if (t.getCellHeightRetryCount.fixed > 10) return;
                            t.getCellHeightRetryCount.fixed++, t._updateFixedCellHeight();
                        }
                    });
                }, a.c.delayTime, "updateFixedCellHeightDelay");
            });
        },
        _updateDynamicCellHeight: function(l) {
            var n = this, r = arguments.length > 1 && void 0 !== arguments[1] ? arguments[1] : "bottom", h = "top" === r, o = this.virtualHeightCacheList, s = h ? [] : o, u = 0;
            this.$nextTick(function() {
                i.u.delay(e(t().mark(function e() {
                    var i, a, g, c, d, v, p;
                    return t().wrap(function(t) {
                        for (;;) switch (t.prev = t.next) {
                          case 0:
                            i = 0;

                          case 1:
                            if (!(i < l.length)) {
                                t.next = 13;
                                break;
                            }
                            return t.next = 4, n._getNodeClientRect("#zp-id-".concat(l[i][n.virtualCellIndexKey]), n.finalUseInnerList);

                          case 4:
                            if (a = t.sent, g = a ? a[0].height : 0, a) {
                                t.next = 8;
                                break;
                            }
                            return t.abrupt("return", void (n.getCellHeightRetryCount.dynamic <= 10 && (o.splice(o.length - i, i), 
                            n.getCellHeightRetryCount.dynamic++, n._updateDynamicCellHeight(l, r))));

                          case 8:
                            c = s.length ? s.slice(-1)[0] : null, d = c ? c.totalHeight : 0, s.push({
                                height: g,
                                lastTotalHeight: d,
                                totalHeight: d + g
                            }), h && (u += g);

                          case 10:
                            i++, t.next = 1;
                            break;

                          case 13:
                            if (h && l.length) {
                                for (v = 0; v < o.length; v++) (p = o[v]).lastTotalHeight += u, p.totalHeight += u;
                                n.virtualHeightCacheList = s.concat(o);
                            }
                            n._updateVirtualScroll(n.oldScrollTop);

                          case 15:
                          case "end":
                            return t.stop();
                        }
                    }, e);
                })), a.c.delayTime, "updateDynamicCellHeightDelay");
            });
        },
        _setCellIndex: function(t) {
            var e = arguments.length > 1 && void 0 !== arguments[1] ? arguments[1] : "bottom", i = 0, n = this.virtualCellIndexKey;
            if (this.totalData.length) {
                if ("bottom" === e) {
                    i = this.realTotalData.length;
                    var r = this.realTotalData.length ? this.realTotalData.slice(-1)[0] : null;
                    r && void 0 !== r[n] && (i = r[n] + 1);
                } else if ("top" === e) {
                    var h = this.realTotalData.length ? this.realTotalData[0] : null;
                    h && void 0 !== h[n] && (i = h[n] - t.length);
                }
            } else this._resetDynamicListState();
            for (var o = 0; o < t.length; o++) {
                var s = t[o];
                s && "[object Object]" === Object.prototype.toString.call(s) || (s = {
                    item: s
                }), s[n] = i + o, s[a.c.listCellIndexUniqueKey] = "".concat(this.virtualListKey, "-").concat(s[n]), 
                t[o] = s;
            }
            this.getCellHeightRetryCount.dynamic = 0, this.cellHeightMode === l.Enum.CellHeightMode.Dynamic && this._updateDynamicCellHeight(t, e);
        },
        _updateVirtualScroll: function(t) {
            var e = arguments.length > 1 && void 0 !== arguments[1] ? arguments[1] : 0, a = i.u.getTime();
            if (0 === t && this._resetTopRange(), !(0 !== t && this.virtualScrollTimeStamp && a - this.virtualScrollTimeStamp <= this.virtualScrollDisTimeStamp)) {
                this.virtualScrollTimeStamp = a;
                var n = 0, r = this.cellHeightMode;
                if (r === l.Enum.CellHeightMode.Fixed) n = parseInt(t / this.virtualCellHeight) || 0, 
                this._updateFixedTopRangeIndex(n), this._updateFixedBottomRangeIndex(n); else if (r === l.Enum.CellHeightMode.Dynamic) {
                    var h = e > 0 ? "top" : "bottom", o = this.virtualRangePageHeight, s = t - o, u = t + this.finalVirtualPageHeight + o, g = 0, c = 0, d = !1, v = this.virtualHeightCacheList, p = v ? v.slice(-1)[0] : null, f = this.virtualTopRangeIndex;
                    if ("bottom" === h) for (var H = f; H < v.length; H++) {
                        var C = v[H];
                        if (C && C.totalHeight > s) {
                            this.virtualTopRangeIndex = H, this.virtualPlaceholderTopHeight = C.lastTotalHeight;
                            break;
                        }
                    } else {
                        for (var m = !1, x = f; x >= 0; x--) {
                            var T = v[x];
                            if (T && T.totalHeight < s) {
                                this.virtualTopRangeIndex = x, this.virtualPlaceholderTopHeight = T.lastTotalHeight, 
                                m = !0;
                                break;
                            }
                        }
                        !m && this._resetTopRange();
                    }
                    for (var L = this.virtualTopRangeIndex; L < v.length; L++) {
                        var y = v[L];
                        if (y && y.totalHeight > u) {
                            g = L, c = p.totalHeight - y.totalHeight, d = !0;
                            break;
                        }
                    }
                    d && 0 !== this.virtualBottomRangeIndex ? (this.virtualBottomRangeIndex = g, this.virtualPlaceholderBottomHeight = c) : (this.virtualBottomRangeIndex = this.realTotalData.length ? this.realTotalData.length - 1 : this.pageSize, 
                    this.virtualPlaceholderBottomHeight = 0), this._updateVirtualList();
                }
            }
        },
        _updateFixedTopRangeIndex: function(t) {
            var e = 0 === this.virtualCellHeight ? 0 : t - (parseInt(this.finalVirtualPageHeight / this.virtualCellHeight) || 1) * this.preloadPage;
            e *= this.virtualListCol, e = Math.max(0, e), this.virtualTopRangeIndex = e, this.virtualPlaceholderTopHeight = e / this.virtualListCol * this.virtualCellHeight;
        },
        _updateFixedBottomRangeIndex: function(t) {
            var e = 0 === this.virtualCellHeight ? this.pageSize : t + (parseInt(this.finalVirtualPageHeight / this.virtualCellHeight) || 1) * (this.preloadPage + 1);
            e *= this.virtualListCol, e = Math.min(this.realTotalData.length, e), this.virtualBottomRangeIndex = e, 
            this.virtualPlaceholderBottomHeight = (this.realTotalData.length - e) * this.virtualCellHeight / this.virtualListCol, 
            this._updateVirtualList();
        },
        _updateVirtualList: function() {
            (this.updateVirtualListFromDataChange || this.lastVirtualTopRangeIndex !== this.virtualTopRangeIndex || this.lastVirtualBottomRangeIndex !== this.virtualBottomRangeIndex) && (this.updateVirtualListFromDataChange = !1, 
            this.lastVirtualTopRangeIndex = this.virtualTopRangeIndex, this.lastVirtualBottomRangeIndex = this.virtualBottomRangeIndex, 
            this.virtualList = this.realTotalData.slice(this.virtualTopRangeIndex, this.virtualBottomRangeIndex + 1));
        },
        _resetDynamicListState: function() {
            var t = arguments.length > 0 && void 0 !== arguments[0] && arguments[0];
            this.virtualHeightCacheList = [], t && (this.virtualList = []), this.virtualTopRangeIndex = 0, 
            this.virtualPlaceholderTopHeight = 0;
        },
        _resetTopRange: function() {
            this.virtualTopRangeIndex = 0, this.virtualPlaceholderTopHeight = 0, this._updateVirtualList();
        },
        _checkVirtualListScroll: function() {
            var t = this;
            this.finalUseVirtualList && this.$nextTick(function() {
                t._getNodeClientRect(".zp-paging-touch-view").then(function(e) {
                    var i = e ? e[0].top : 0;
                    (!e || i === t.pagingOrgTop && 0 !== t.virtualPlaceholderTopHeight) && t._updateVirtualScroll(0);
                });
            });
        },
        _innerCellClick: function(t, e) {
            this.$emit("innerCellClick", t, e);
        }
    }
};

exports.virtualListModule = n;